'use strict';

/**
 * Config for the router
 */
angular.module('app')
  .run(['$rootScope', '$state', '$stateParams', '$http', '$cookies', 'authenticationService',
      function ($rootScope, $state, $stateParams, $http, $cookies, authenticationService) {
          $http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
          $http.defaults.headers.common['RequestVerificationToken'] = angular.element("body").attr('ncg-request-verification-token');
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
          $rootScope.$on('$stateChangeError', function (e, toState, toParams, fromState, fromParams, error) {
              //console.info("Error: " + error);
              if (error === "Not Authorized") {
                  $state.go("access.signin");
              }
          });
      }
  ])
  .config(['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $urlRouterProvider
            .otherwise('/app/management/user');

        $stateProvider.decorator('data', function (state, parent) {
            var stateData = state.parent;
            var data = stateData.data || {};
            var requiredLogin = stateData.self.requiredLogin || false;
            //console.info(state, stateData, requiredLogin);

            state.resolve = state.resolve || {};
            if (requiredLogin) {
                // defined (flag) && flag && logged in
                state.resolve.security = ['$q', "authenticationService", function ($q, authenticationService) {
                    var athenticateResult = false;
                    athenticateResult = authenticationService.isLogged;
                    //console.info("Trying to athenticate...", athenticateResult);
                    if (!athenticateResult) {
                        return $q.reject("Not Authorized");
                    }
                }];
            }

            return stateData;
        });

        $stateProvider
            .state('app', {
                abstract: true,
                url: '/app',
                templateUrl: 'tpl/app.html',
                requiredLogin: true
            })
            .state('app.dashboard-v1', {
                url: '/dashboard-v1',
                templateUrl: 'tpl/app_dashboard_v1.html',
                resolve: {
                    deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load(['js/controllers/chart.js']);
                    }]
                }
            })

            // Management
            .state('app.management', {
                url: '/management',
                template: '<div ui-view class="fade-in"></div>',
                resolve: {
                    deps: ['uiLoad',
                        function (uiLoad) {
                            return uiLoad.load('js/controllers/form.js');
                        }]
                }
            })
            .state('app.management.user', {
                url: '/user',
                templateUrl: 'tpl/app_management_user.html',
                resolve: {
                    deps: ['$ocLazyLoad', 'uiLoad',
                    function ($ocLazyLoad, uiLoad) {
                        return $ocLazyLoad.load('ngGrid').then(
                            function () {
                                return $ocLazyLoad.load('js/controllers/management/userCtrl.js');
                            }
                        );
                    }]
                }
            })
            .state('app.management.user_edit', {
                url: '/user_edit',
                templateUrl: 'tpl/app_management_user_edit.html',
                params: {
                    'UserId': null,
                    'RealName': null,
                    'UserName': null,
                    'PhoneNumber': null,
                    'Roles':null
                },
                resolve: {
                    deps: ['$ocLazyLoad', 'uiLoad',
                    function ($ocLazyLoad, uiLoad) {
                        return $ocLazyLoad.load(['azureBlobUpload', 'angularFileUpload', 'ngGrid']).then(
                            function () {
                                return $ocLazyLoad.load('js/controllers/management/userCtrl.js');
                            }
                        );
                    }]
                }
            })
            .state('app.management.order', {
                url: '/order',
                templateUrl: 'tpl/app_management_order.html',
                resolve: {
                    deps: ['$ocLazyLoad', 'uiLoad',
                    function ($ocLazyLoad, uiLoad) {
                        return $ocLazyLoad.load('ngGrid').then(
                            function () {
                                return $ocLazyLoad.load('js/controllers/management/orderCtrl.js');
                            }
                        );
                    }]
                }
            })
             .state('app.management.order_detail', {
                 url: '/order_detail',
                 templateUrl: 'tpl/app_management_order_detail.html',
                 params: {
                     'Id':null,
                     'Contacts': null,
                     'Amount': null,
                     'CarLength': null,
                     'Category': null,
                     'ContactsChatId': null,
                     'Telephone': null,
                     'DeliveryTime': null,
                     'ArrivalTime':null
                 },
                 resolve: {
                     deps: ['$ocLazyLoad', 'uiLoad',
                     function ($ocLazyLoad, uiLoad) {
                         return $ocLazyLoad.load('ngGrid').then(
                             function () {
                                 return $ocLazyLoad.load('js/controllers/management/orderCtrl.js');
                             }
                         );
                     }]
                 }
             })
             .state('app.management.carrierOrder', {
                 url: '/carrierOrder',
                 templateUrl: 'tpl/app_management_carrierOrder.html',
                 resolve: {
                     deps: ['$ocLazyLoad', 'uiLoad',
                     function ($ocLazyLoad, uiLoad) {
                         return $ocLazyLoad.load('ngGrid').then(
                             function () {
                                 return $ocLazyLoad.load('js/controllers/management/carrierOrderCtrl.js');
                             }
                         );
                     }]
                 }
             })
            .state('app.management.realname', {
                url: '/realname',
                templateUrl: 'tpl/app_management_realname.html',
                resolve: {
                    deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load('ngGrid').then(
                            function () {
                                return $ocLazyLoad.load('js/controllers/management/realNameCtrl.js');
                            }
                        );
                    }]
                }
            })

            // Settings
            .state('app.settings', {
                url: '/settings',
                templateUrl: 'tpl/app_settings.html',
                resolve: {
                    deps: ['$ocLazyLoad',
                    function ($ocLazyLoad) {
                        return $ocLazyLoad.load('ngGrid').then(
                            function () {
                                return $ocLazyLoad.load('js/controllers/settings/settingsCtrl.js');
                            }
                        );
                    }]
                }
            })

            // Access
            .state('lockme', {
                url: '/lockme',
                templateUrl: 'tpl/page_lockme.html',
                requiredLogin: false
            })
            .state('access', {
                url: '/access',
                template: '<div ui-view class="fade-in-right-big smooth"></div>',
                requiredLogin: false
            })
            .state('access.signin', {
                url: '/signin',
                templateUrl: 'tpl/page_signin.html',
                requiredLogin: false,
                resolve: {
                    deps: ['uiLoad',
                    function (uiLoad) {
                        return uiLoad.load(['js/controllers/signin.js']);
                    }]
                }
            })
            .state('access.signup', {
                url: '/signup',
                templateUrl: 'tpl/page_signup.html',
                requiredLogin: false,
                resolve: {
                    deps: ['uiLoad',
                    function (uiLoad) {
                        return uiLoad.load(['js/controllers/signup.js']);
                    }]
                }
            })
            .state('access.forgotpwd', {
                url: '/forgotpwd',
                templateUrl: 'tpl/page_forgotpwd.html',
                requiredLogin: false
            })
            .state('access.404', {
                url: '/404',
                templateUrl: 'tpl/page_404.html',
                requiredLogin: false
            })
    }]
);